<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <div>
         <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :immediate-check="false"
      offset="50"
      @load="onLoad"
    >
       <article-item
        v-for="item in articleList"
        :key="item.art_id"
        :obj="item"
        :showX="false"
        @click.native="$router.push(`/article_detail?aid=${item.art_id}`)"
      />
       </van-list>
      </div>
    </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import { searchResultListAPI } from '@/api'
import ArticleItem from '@/views/Home/components/ArticleItem'
export default {
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async getArticlesList () {
      const res = await searchResultListAPI({
        q: this.$route.params.keywords,
        page: this.page
      })
      // console.log(res
      console.log(res)
      res.data.data.results.forEach((item) => {
        item.pubdate = timeAgo(item.pubdate)
      })
      if (res.data.data.results.length === 0) {
        this.loading = false
        this.finished = true
      } else {
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
    },
    onLoad () {
      if (this.articleList.length > 0) {
        this.page++
      }
      this.getArticlesList()
    }
  },
  components: { ArticleItem },
  name: 'SearchResult',
  async created () {
    this.getArticlesList()
  }
}

</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
